package com.sq.partner.manager.service.impl;

import com.sq.partner.manager.common.DictCache;
import com.sq.partner.manager.common.Result;
import com.sq.partner.manager.dao.CarAccidentDao;
import com.sq.partner.manager.dao.ICoreDeptDao;
import com.sq.partner.manager.mybatis.model.CoreDept;
import com.sq.partner.manager.mybatis.model.DictDto;
import com.sq.partner.manager.mybatis.model.TradConfig;
import com.sq.partner.manager.service.CarAccidentService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StreamUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 2022/9/14 16:21
 */
@Service
public class CarAccidentServiceImpl implements CarAccidentService {
	@Autowired
	private ICoreDeptDao coreDeptDao;
	@Autowired
	private CarAccidentDao carAccidentDao;

	private static Map<String,String> vhlTypeDict = new HashMap<String, String>();
	@Override
	public List<CoreDept> queryPrivilegeBranch(String deptCodeStr)
			throws Exception {

		return coreDeptDao.queryDeptList(deptCodeStr);
	}

	@Override
	public Page<Map<String, Object>> queryConfigPlanByCriteria(Page<Map<String, Object>> page) throws Exception {
		Map<String, Object> map = page.getMap();
		Integer count = carAccidentDao.queryConfigPlanCount(map);
		List<Map<String,Object>> list =  carAccidentDao.queryConfigPlanByCriteria(map);
		for (Map<String, Object> obj : list) {
			//数据库中的值
			String vhlTyp = (String) obj.get("vhlTyp");
			vhlTyp = vhlTyp.replace(",","").trim();
			//最终返回前端的值
			String vhlTypStr="";
			//特殊车辆使用性质
			List<DictDto> specialVhlType = DictCache.getDict("specialVhlType");
			if (specialVhlType.size() > 0){
				for (DictDto dictDto : specialVhlType) {
					if (vhlTyp.contains(dictDto.getCode())) {
						vhlTyp = vhlTyp.replaceAll(dictDto.getCode(),"");
						vhlTypStr += dictDto.getCodeName();
					}
				}
			}
			//6位普通车辆使用性质
			for (int i = 0; i < vhlTyp.length(); i+=6) {
				String s = vhlTyp.substring(i,i+6);
				if (vhlTypeDict.size() != 0){
					s = vhlTypeDict.get(s);
					vhlTypStr += s;
				}else {
					vhlTypeDict();
				}
			}
			obj.put("vhlTyp",vhlTypStr);
		}
		page.setData(list);
		page.setTotal(count);
		return page;
	}

	@Override
	public Result updateConfigPlanList(List<TradConfig> configArrayList) throws Exception {
		for (TradConfig tradConfig : configArrayList) {
			System.out.println(tradConfig.getSerialNumber());
			if (tradConfig.getSerialNumber() != null && tradConfig.getSerialNumber() != ""){
				Pattern pattern = Pattern.compile("^(0|[1-9][0-9]*)$");
				Matcher matcher = pattern.matcher(tradConfig.getSerialNumber());
				if (!matcher.matches()) {
					return Result.error("序号仅可填写数字，请检查");
				}
			}else if (tradConfig.getSerialNumber() == null){
				tradConfig.setSerialNumber("");
			}

		}
		Boolean flag = carAccidentDao.updateConfigPlanList(configArrayList);
		if (flag){
			return Result.success("更新成功");
		}
		return Result.error("更新失败");
	}

	private void vhlTypeDict(){
		vhlTypeDict.put("309001","家庭自用车，");
		vhlTypeDict.put("309002","机关、事业单位用车，");
		vhlTypeDict.put("309003","营业客车，");
		vhlTypeDict.put("309004","企业非营业车，");
		vhlTypeDict.put("309005","特种车，");
		vhlTypeDict.put("309008","摩托车，");
		vhlTypeDict.put("309009","拖拉机，");
		vhlTypeDict.put("309012","城市公交营业客车，");
		vhlTypeDict.put("309014","非营业货车，");
		vhlTypeDict.put("309015","营业货车，");
		vhlTypeDict.put("309016","出租、租赁，");
		vhlTypeDict.put("309017","公路客运，");
		vhlTypeDict.put("309018","旅游客运，");
		vhlTypeDict.put("309035","非营业挂车，");
		vhlTypeDict.put("309036","营业挂车，");
		vhlTypeDict.put("309041","特种车挂车，");
		vhlTypeDict.put("309040","低速载货和三轮汽车，");
		vhlTypeDict.put("301006001","摩托车，");
		vhlTypeDict.put("301006002","拖拉机，");
		vhlTypeDict.put("309039","货车，");
	}
}
